# Maintainer: Sergey Popov <popov.sa@gmail.com>

_realname=itk
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=5.3.0
pkgrel=2
pkgdesc='An open-source C++ toolkit for medical image processing (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64')
url='https://www.itk.org/'
license=('Apache')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-double-conversion"
         "${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-fftw"
         "${MINGW_PACKAGE_PREFIX}-gdcm"
         "${MINGW_PACKAGE_PREFIX}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-eigen3"
             "${MINGW_PACKAGE_PREFIX}-gtest"
             "${MINGW_PACKAGE_PREFIX}-opencv"
             "${MINGW_PACKAGE_PREFIX}-vtk"
             "${MINGW_PACKAGE_PREFIX}-adios2"
             "${MINGW_PACKAGE_PREFIX}-cgns"
             "${MINGW_PACKAGE_PREFIX}-ffmpeg"
             "${MINGW_PACKAGE_PREFIX}-cli11"
             "${MINGW_PACKAGE_PREFIX}-gdal"
             "${MINGW_PACKAGE_PREFIX}-gl2ps"
             "${MINGW_PACKAGE_PREFIX}-libmariadbclient"
             "${MINGW_PACKAGE_PREFIX}-liblas"
             "${MINGW_PACKAGE_PREFIX}-libogg"
             "${MINGW_PACKAGE_PREFIX}-libtheora"
             "${MINGW_PACKAGE_PREFIX}-openslide"
             "${MINGW_PACKAGE_PREFIX}-openturns"
             "${MINGW_PACKAGE_PREFIX}-openvdb"
             "${MINGW_PACKAGE_PREFIX}-openvr"
             "${MINGW_PACKAGE_PREFIX}-pdal"
             "${MINGW_PACKAGE_PREFIX}-proj"
             "${MINGW_PACKAGE_PREFIX}-qt6-base"
             "${MINGW_PACKAGE_PREFIX}-qt6-declarative"
             "${MINGW_PACKAGE_PREFIX}-unixodbc"
             "${MINGW_PACKAGE_PREFIX}-utf8cpp")
optdepends=("${MINGW_PACKAGE_PREFIX}-opencv: ITK-OpenCV bridge"
            "${MINGW_PACKAGE_PREFIX}-vtk: ITK-VTK bridge")
source=("https://github.com/InsightSoftwareConsortium/ITK/releases/download/v${pkgver}/InsightToolkit-${pkgver}.tar.gz")
sha256sums=('57a4471133dc8f76bde3d6eb45285c440bd40d113428884a1487472b7b71e383')

build() {
  [[ -d "${srcdir}/build-${MSYSTEM}" ]] && rm -rf "${srcdir}/build-${MSYSTEM}"
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  local -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DBUILD_SHARED_LIBS=ON \
    -DBUILD_DOCUMENTATION=OFF \
    -DBUILD_EXAMPLES=OFF \
    -DBUILD_TESTING=OFF \
    -DITK_INSTALL_INCLUDE_DIR=include/ITK \
    -DITK_INSTALL_DATA_DIR=share/ITK \
    -DITK_INSTALL_DOC_DIR=share/doc/ITK \
    -DITK_INSTALL_PACKAGE_DIR=lib/cmake/ITK \
    -DITK_CUSTOM_LIBRARY_SUFFIX="" \
    -DITK_LEGACY_REMOVE=OFF \
    -DITK_BUILD_DEFAULT_MODULES=OFF \
    -DITKGroup_Core=ON \
    -DITKGroup_IO=$([[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]] && echo "ON" || echo "OFF") \
    -DITKGroup_Filtering=ON \
    -DITKGroup_Registration=ON \
    -DITKGroup_Segmentation=ON \
    -DITKGroup_Numerics=ON \
    -DITKGroup_Video=ON \
    -DITKGroup_ThirdParty=ON \
    -DITKGroup_Bridge=ON \
    -DITKGroup_Nonunit=$([[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]] && echo "ON" || echo "OFF") \ \
    -DITKGroup_Compatibility=ON \
    -DModule_ITKVideoBridgeOpenCV=ON \
    -DModule_ITKVtkGlue=ON \
    -DITK_USE_GPU=OFF \
    -DUSE_FFTWD=ON \
    -DUSE_FFTWF=ON \
    -DITK_USE_SYSTEM_LIBRARIES=ON \
    -DITK_USE_SYSTEM_DOUBLECONVERSION=ON \
    -DITK_USE_SYSTEM_EIGEN=ON \
    -DITK_USE_SYSTEM_EXPAT=ON \
    -DITK_USE_SYSTEM_FFTW=ON \
    -DITK_USE_SYSTEM_GOOGLETEST=ON \
    -DITK_USE_SYSTEM_HDF5=ON \
    -DITK_USE_SYSTEM_JPEG=ON \
    -DITK_USE_SYSTEM_PNG=ON \
    -DITK_USE_SYSTEM_TIFF=ON \
    -DITK_USE_SYSTEM_VXL=OFF \
    -DITK_USE_SYSTEM_ZLIB=ON \
    -DITK_USE_SYSTEM_GDCM=ON \
    -DITK_SKIP_PATH_LENGTH_CHECKS=1 \
    -DITK_FORBID_DOWNLOADS=ON \
    -DVNL_CONFIG_THREAD_SAFE=ON \
    -DVNL_CONFIG_ENABLE_SSE2_ROUNDING=ON \
    "${_extra_config[@]}" \
    "../InsightToolkit-${pkgver}"

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR=${pkgdir} ${MINGW_PREFIX}/bin/cmake --install .

  cd "${pkgdir}${MINGW_PREFIX}"
  local PREFIX_DEPS=$(cygpath -am ${MINGW_PREFIX})
  sed -s "s|${PREFIX_DEPS}|\${_IMPORT_PREFIX}|g" \
    -i "lib/cmake/${_realname}/ITKTargets-release.cmake"
  find "lib/cmake/${_realname}" -name '*.cmake' -exec \
    sed -s "s|${PREFIX_DEPS}|\${ITK_INSTALL_PREFIX}|g" -i {} \;

  install -Dm644 "${srcdir}/InsightToolkit-${pkgver}/LICENSE" "share/licenses/${_realname}/LICENSE"
  install -Dm644 "${srcdir}/InsightToolkit-${pkgver}/NOTICE" "share/licenses/${_realname}/NOTICE"
  install -Dm644 "${srcdir}/InsightToolkit-${pkgver}/Modules/ThirdParty/KWSys/src/KWSys/Copyright.txt" \
    "share/licenses/${_realname}/KWSys/Copyright.txt"
}
